package org.genntii.warehousesystem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yitter.idgen.YitIdHelper;
import jakarta.annotation.Resource;
import org.genntii.warehousesystem.Exception.ParamErrorException;
import org.genntii.warehousesystem.domain.entity.*;
import org.genntii.warehousesystem.domain.vo.*;
import org.genntii.warehousesystem.mapper.*;
import org.genntii.warehousesystem.service.GoodsWarehouseService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsWarehouseServiceImpl extends ServiceImpl<GoodsWarehouseMapper, GoodsWarehouse>implements GoodsWarehouseService {

    @Resource
    private GoodsWarehouseMapper goodsWarehouseMapper;

    @Resource
    private GoodsShipmentMapper shipmentMapper;

    @Resource
    private GoodsProcurementMapper procurementMapper;

    @Resource
    private GoodsRestockMapper restockMapper;

    @Resource
    private WarehouseMapper warehouseMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private EmployeeMapper employeeMapper;

    @Override
    public void create(int goodsId, int warehouseId) {
        GoodsWarehouse gw = new GoodsWarehouse();
        gw.setGoodsId(goodsId);
        gw.setWarehouseId(warehouseId);
        gw.setGoodsNum(0);
        goodsWarehouseMapper.insert(gw);
    }

    @Override
    public List<SimpleGoodsVO> selectGoodsByWarehouse(int warehouseId) {
        List<Integer> goodsIds = goodsWarehouseMapper.selectGoodsByWarehouse(warehouseId);
        List<SimpleGoodsVO> voList = new ArrayList<>();
        for (int i : goodsIds){
            Goods goods = goodsMapper.selectById(i);
            SimpleGoodsVO vo = SimpleGoodsVO.builder().id(i).name(goods.getName()).imgSrc(goods.getImgSrc())
                    .price(goods.getOutPrice()).wholeSalePrice(goods.getWholesalePrice()).build();
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public List<GoodsWarehouseVO> selectByGoods(int goodsId) {
        List<GoodsWarehouse> goodsWarehouseList = goodsWarehouseMapper.selectGoodsWarehouseByGoodsId(goodsId);
        List<GoodsWarehouseVO> voList = new ArrayList<>();
        for (GoodsWarehouse gw:goodsWarehouseList){
            GoodsWarehouseVO vo = new GoodsWarehouseVO();
            vo.setWarehouseName(warehouseMapper.selectById(gw.getWarehouseId()).getName());
            vo.setNum(gw.getGoodsNum());
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public void procurement(int goodsId,int warehouseId, int operatorId, int num, float price) {
        GoodsProcurement gp = GoodsProcurement.builder().wid(String.valueOf(YitIdHelper.nextId()))
                .goodsId(goodsId).warehouseId(warehouseId)
                .operatorId(operatorId).num(num)
                .price(BigDecimal.valueOf(price))
                .inDate(LocalDateTime.now())
                .build();

        GoodsWarehouse goodsWarehouse = goodsWarehouseMapper.selectNum(goodsId,warehouseId);
        int total = goodsWarehouse.getGoodsNum()+num;
        if (total < 0){
            throw new ParamErrorException("参数错误");
        }
        procurementMapper.insert(gp);
        goodsWarehouseMapper.setNum(total,goodsWarehouse.getId());

    }

    @Override
    public List<ProcurementVO> listProcurement(int page) {
        List<GoodsProcurement> goodsProcurementList = procurementMapper.listByPage(10*(page-1));
        List<ProcurementVO> voList = new ArrayList<>();
        for (GoodsProcurement gp:goodsProcurementList){
            ProcurementVO vo = new ProcurementVO();
            vo.setId(gp.getId());
            vo.setWid(gp.getWid());
            vo.setGoods(goodsMapper.selectById(gp.getGoodsId()).getName());
            vo.setWarehouse(warehouseMapper.selectById(gp.getWarehouseId()).getName());
            vo.setOperator(employeeMapper.selectById(gp.getOperatorId()).getName());
            vo.setPrice(gp.getPrice());
            vo.setDateTime(gp.getInDate());
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public void restock(int goodsId, int fromWarehouseId, int toWarehouseId, int num, int operatorId) {
        GoodsRestock gr = GoodsRestock.builder().wid(String.valueOf(YitIdHelper.nextId()))
                .goodsId(goodsId).fromWarehouseId(fromWarehouseId).toWarehouseId(toWarehouseId)
                .num(num).operatorId(operatorId)
                .build();
        GoodsWarehouse from = goodsWarehouseMapper.selectNum(goodsId,fromWarehouseId);
        GoodsWarehouse to = goodsWarehouseMapper.selectNum(goodsId,toWarehouseId);
        int totalFrom = from.getGoodsNum()-num;
        if (totalFrom < 0){
            throw new ParamErrorException("数据错误");
        }
        int totalTo = to.getGoodsNum()+num;
        goodsWarehouseMapper.setNum(totalFrom,from.getId());
        goodsWarehouseMapper.setNum(totalTo,to.getId());
        restockMapper.insert(gr);
    }

    @Override
    public List<RestockVO> listRestock(int page) {
        List<GoodsRestock> goodsRestocks = restockMapper.listByPage(10*(page-1));
        List<RestockVO> voList = new ArrayList<>();
        for (GoodsRestock gr:goodsRestocks){
            RestockVO vo = new RestockVO();
            vo.setId(gr.getId());
            vo.setWid(gr.getWid());
            vo.setFromWarehouse(warehouseMapper.selectById(gr.getFromWarehouseId()).getName());
            vo.setToWarehouse(warehouseMapper.selectById(gr.getToWarehouseId()).getName());
            vo.setGoods(goodsMapper.selectById(gr.getGoodsId()).getName());
            vo.setNum(gr.getNum());
            vo.setOperator(employeeMapper.selectById(gr.getOperatorId()).getName());
            vo.setDateTime(gr.getCreateTime());
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public void shipment(Long orderCode, int warehouseId, int operatorId, int goodsId, int num) {
        GoodsShipment gs = GoodsShipment.builder().orderCode(orderCode).warehouseId(warehouseId)
                .operatorId(operatorId).goodsId(goodsId).num(num).shipmentTime(LocalDateTime.now()).build();
        GoodsWarehouse gw = goodsWarehouseMapper.selectNum(goodsId,warehouseId);
        int total = gw.getGoodsNum()-num;
        if (total < 0) throw new ParamErrorException("参数错误");
        goodsWarehouseMapper.setNum(total,gw.getId());
        shipmentMapper.insert(gs);
    }

    @Override
    public List<ShipmentVO> listShipment(int page) {
        List<GoodsShipment> goodsShipments = shipmentMapper.listByPage(10*(page-1));
        List<ShipmentVO> voList = new ArrayList<>();
        for (GoodsShipment gs: goodsShipments){
            ShipmentVO vo = new ShipmentVO();
            vo.setId(gs.getId());
            vo.setOrderCode(String.valueOf(gs.getOrderCode()));
            vo.setWarehouse(warehouseMapper.selectById(gs.getWarehouseId()).getName());
            vo.setOperator(employeeMapper.selectById(gs.getOperatorId()).getName());
            vo.setGoods(goodsMapper.selectById(gs.getGoodsId()).getName());
            vo.setNum(gs.getNum());
            vo.setDateTime(gs.getShipmentTime());
            voList.add(vo);
        }
        return voList;
    }

}
